const express=require('express');
const expressStatic=require('express-static');
const slideRouter=require('./router/slide');
const userRouter=require('./router/user');
const viewsRouter=require('./router/views');
const childRouter=require('./router/child');
const heRouter=require('./router/he');
const sheRouter=require('./router/she');
const mobRouter=require('./router/mob');
const dbConnect=require('./utils/db/mongdbConnect');
const bodyParser=require('body-parser')
const cookieParser=require('cookie-parser');
const cookieSession=require('cookie-session');

var server=express();

server.listen(8080,function (err) {
    if(!err){
        console.log('服务器开启成功！');
    }else {
        console.log('服务器启动失败！');
    }
});
dbConnect.dbConnect();

server.use(bodyParser.urlencoded({extend:true}));
//启用cookie
var keys=[];
for(var i=0;i<10000;i++){
    var str='abcd133%%^&'+Math.random(1000000);
    keys.push(str);
}
server.use(cookieParser());
server.use(cookieSession({
    name:'storeOnline',
    keys:keys,
    maxAge:20*60*1000
}));

server.use('/slide',slideRouter.router);
server.use('/user',userRouter.router);
server.use('/views',viewsRouter.router);
server.use('/child',childRouter.router);
server.use('/he',heRouter.router);
server.use('/she',sheRouter.router);
server.use('/mob',mobRouter.router);

server.use('/',function (req,res,next) {
    var options={
        root:__dirname+'/www/',
        dotfiles:'deny',
        headers:{
            'x-timestamp':Date.now(),
            'x-sent':true
        }
    };
    var url=req.url;
    if(url.indexOf('/')==0&&url.length>1){
        next();
    }else{
        res.sendFile('index.html',options,function (err) {
            if(err){
                console.log('index.html is error!');
            }
        });
    }
});
server.use(expressStatic('./www'));